package es

import (
	"context"
	"github.com/olivere/elastic/v7"
	"log"
	"os"
)

var Client *elastic.Client
var host = "http://120.27.208.86:9200/"

// 初始化
func init() {
	errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
	var err error
	Client, err = elastic.NewClient(elastic.SetErrorLog(errorlog), elastic.SetURL(host), elastic.SetSniff(false))
	if err != nil {
		return
	}
}

// elasticSearch 查询数据
func ElasticSearchSearch(index string, query elastic.Query) (*elastic.SearchResult, error) {
	result, err := Client.Search().Index(index).Query(query).Do(context.Background())
	if err != nil {
		return nil, err
	}
	return result, nil
}

func ElasticSearchSearchWithHighlight(index string, query elastic.Query, highlight *elastic.Highlight) (*elastic.SearchResult, error) {
	elastic.NewHighlight().Field("VideoName")
	elastic.NewMatchQuery("VideoName", "")
	result, err := Client.Search(index).Query(query).Highlight(highlight).Do(context.Background())
	if err != nil {
		return nil, err
	}
	return result, nil
}
